// pages/auto/getuserinfo.js
const app = getApp();
import config from '../../utils/config.js'
var Base64 = require("../../utils/base64.js");
var CID = config.CID;
var version = config.version;
var logo = config.logo;
var WEBSITENAME = config.getWebsiteName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: logo,
    WEBSITENAME: WEBSITENAME,
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getuserinfo:function(res){
    console.log(res.detail);
    if (res.detail.userInfo!=undefined){
      console.log('has');
      try {
        wx.setStorageSync('nickName', res.detail.userInfo.nickName);
        wx.setStorageSync('avatarUrl', res.detail.userInfo.avatarUrl);
        wx.setStorageSync('gender', res.detail.userInfo.gender);
        wx.setStorageSync('province', res.detail.userInfo.province);
        wx.setStorageSync('city', res.detail.userInfo.city);
        wx.setStorageSync('country', res.detail.userInfo.country);
        wx.setStorageSync('language', res.detail.userInfo.language);
        wx.setStorageSync('userInfo', res.detail.userInfo);
      } catch (e) {

      }
      // 注册用户并获取token
      wx.login({   // 获取code
        success: res => {
          if (res.code) {
            wx.request({ // 获取openid
              url: app.globalData.api + '/wx/openid.html',
              method: 'GET',
              data: {
                code: res.code
              },
              success: res => {
                if (res.data.code == 0) {
                  console.log('res===', res.data)
                  wx.setStorageSync('openid', res.data.data.openid);
                  wx.setStorageSync('token', res.data.data.token);
                  this.fetchUserinfo();
                 
                }
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg);
            reject('error');
          }
        }
      })
    
    }
    else{
      console.log('授权失败');
      wx.showToast({
        title: '授权失败',
        icon:'none',
      })
    }





  },
  fetchUserinfo: function () {
    // 注册用户并获取token
    wx.request({
      url: app.globalData.api + '/Users/index.html&token=' + wx.getStorageSync('token'),
      method: 'POST',
      data: {
        'nickName': wx.getStorageSync('nickName'),
        'avatarUrl': wx.getStorageSync('avatarUrl'),
        'gender': wx.getStorageSync('gender'),
        'province': wx.getStorageSync('province'),
        "city": wx.getStorageSync('city'),
        "country": wx.getStorageSync('country'),
        "language": wx.getStorageSync('language'),
        'openid': wx.getStorageSync('openid'),
      },
      success: res => {
        if (res.data.code == 0) {
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
  }
})